package com.eachonline.smart.web.course;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.eachonline.core.Pagination;
import com.eachonline.smart.mapper.SmartClassTeacherMapper;
import com.eachonline.smart.model.SmartClassTeacher;
import com.eachonline.smart.model.SmartClassTeacherExample;
import com.eachonline.smart.model.SmartCourse;
import com.eachonline.smart.model.SmartUser;
import com.eachonline.smart.service.ClassTeacherService;
import com.eachonline.smart.web.common.CommonUtil;
import com.eachonline.util.StringUtil;
import com.eachonline.util.UUIDUtil;

@Controller
@RequestMapping("/class")
public class SmartClassController{
	
	
	@Resource
	private ClassTeacherService classTeacherService;
	
	
	@Autowired
	private SmartClassTeacherMapper  classTeacherMapper;
	

	/**
	 * 班级创建和修改
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/createAndupdateClass",method = RequestMethod.POST)
	public JsonResult createClass(HttpServletRequest request,HttpServletResponse  response,SmartClassTeacher  teacherClass){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		Map<String,Object> dataMap=new HashMap<String, Object>();
		if(StringUtil.isEmpty(teacherClass.getClassName()))
		{
			return new JsonResult(false, "班级名称不能为空");
		}
		
		if(StringUtil.isEmpty(teacherClass.getClassName()))
		{
			return new JsonResult(false, "班级主图不能为空");
		}
		
		if(StringUtil.isEmpty(teacherClass.getClassTeacher()))
		{
			return new JsonResult(false, "班级老师不能为空");
		}
		
		if(StringUtil.isEmpty(teacherClass.getClassNum()+""))
		{
			return new JsonResult(false, "班级人数不能为空");
		}
		
		
		if(StringUtil.isEmpty(teacherClass.getSid()))
		{
			teacherClass.setSid(UUIDUtil.getUUID());
			teacherClass.setCreateTime(new Date());
			classTeacherService.insert(teacherClass);
			dataMap.put("class", teacherClass);
			return  new JsonResult(true, "成功",dataMap);
		}else
		{
			classTeacherService.updateByPrimaryKeySelective(teacherClass);
			dataMap.put("class", teacherClass);
			return  new JsonResult(true, "成功",dataMap);
		}
		
		
	
	}
	
	
	
	/**
	 * 查询课程对应的班级Id
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryClassId",method = RequestMethod.POST)
	public JsonResult queryClassId(HttpServletRequest request,HttpServletResponse  response,String   courseId){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		if(StringUtil.isEmpty(courseId))
		{
			  return  new JsonResult(false, "课程Id不存在"); 
		}
		SmartClassTeacherExample  scte=new SmartClassTeacherExample();
		scte.createCriteria().andCourseIdEqualTo(courseId);
		List<SmartClassTeacher> smartClassList=	classTeacherService.selectByExample(scte);
		
		if(smartClassList.isEmpty())
		{
			 return  new JsonResult(false, "班级不存在"); 
		}
		Map<String,Object> dataMap=new HashMap<String, Object>();
		dataMap.put("classId", smartClassList.get(0).getSid());
		return  new JsonResult(true, "成功",dataMap); 
	}
	
	/**
	 * 查询课程对应的班级Id
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryClassInfo",method = RequestMethod.POST)
	public JsonResult queryClassInfo(HttpServletRequest request,HttpServletResponse  response,String   courseId){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		if(StringUtil.isEmpty(courseId))
		{
			  return  new JsonResult(false, "课程Id不存在"); 
		}
		SmartClassTeacherExample  scte=new SmartClassTeacherExample();
		scte.createCriteria().andCourseIdEqualTo(courseId);
		List<SmartClassTeacher> smartClassList=	classTeacherService.selectByExample(scte);
		
		if(smartClassList.isEmpty())
		{
			 return  new JsonResult(false, "班级不存在"); 
		}
		
		SmartClassTeacher classDetail=classTeacherService.selectByPrimaryKey(smartClassList.get(0).getSid());
		if(classDetail==null)
		{
			return new JsonResult(false, "班级不存在");
		}else
		{
			Map<String,Object> dataMap=new HashMap<String, Object>();
			dataMap.put("classDetail", classDetail);
			return  new JsonResult(true, "成功",dataMap);
		}
		
		
	}
	
	/**
	 * 班级删除
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteClass",method = RequestMethod.POST)
	public JsonResult deleteClass(HttpServletRequest request,HttpServletResponse  response,String   classId){
		
		
	   int row=	classTeacherService.deleteByPrimaryKey(classId);
	   if(row>0)
	   {
		   return  new JsonResult(true, "成功"); 
	   }
	   
	   return  new JsonResult(false, "失败"); 
	}
	
	

	/**
	 * 查询教师班级详情
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryClass",method = RequestMethod.POST)
	public JsonResult querySmartClassDetail(HttpServletRequest request,HttpServletResponse  response,String classId){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
	
		SmartClassTeacher classDetail=classTeacherService.selectByPrimaryKey(classId);
		if(classDetail==null)
		{
			return new JsonResult(false, "班级不存在");
		}else
		{
			Map<String,Object> dataMap=new HashMap<String, Object>();
			dataMap.put("classDetail", classDetail);
			return  new JsonResult(true, "成功",dataMap);
		}
		
	
	}
	
	/**
	 * 查询教师班级列表
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryTeacherClassList",method = RequestMethod.POST)
	public JsonResult queryHomeCourseList(HttpServletRequest request,HttpServletResponse  response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		SmartUser currentUser=CommonUtil.getCurrentUser(request);
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		int begin =pageSize*(start-1);
		
		SmartClassTeacherExample  ctEx=new SmartClassTeacherExample();
		ctEx.createCriteria().andUserIdEqualTo(currentUser.getSid());
		Pagination<SmartClassTeacher> classPageInfo= classTeacherService.queryByPage(ctEx, begin, pageSize," create_time desc");
		Map<String,Object> resultMap=new HashMap<String, Object>();
		resultMap.put("classInfo", classPageInfo);
		return new JsonResult(true, "成功", resultMap);
	 
	}
	
	
	/**
	 * 查询学生班级列表
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryStudentClassList",method = RequestMethod.POST)
	public JsonResult queryStudentClassList(HttpServletRequest request,HttpServletResponse  response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		SmartUser currentUser=CommonUtil.getCurrentUser(request);
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		int begin =pageSize*(start-1);
		Pagination<SmartCourse >  page=new Pagination<SmartCourse>(begin, pageSize);
		SmartClassTeacherExample  ctEx=new SmartClassTeacherExample();
		ctEx.setUserId(currentUser.getSid());
		ctEx.setPagination(page);
		List<SmartClassTeacher> classlist= classTeacherMapper.queryStudentClasses(ctEx);
		int sumRow= classTeacherMapper.queryStudentClassesCount(ctEx);
		Map<String,Object> resultMap=new HashMap<String, Object>();
		resultMap.put("classInfo", classlist);
		resultMap.put("sumRow", sumRow);
		return new JsonResult(true, "成功", resultMap);
	 
	}
	

	
}
